package com.ptswitch.material.pages.contract;

import java.util.Date;
import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.AppRuntimeException;
import org.hxzon.util.BigDecimalUtil;
import org.hxzon.util.EMailUtil;
import org.hxzon.util.EMailUtil.EMail;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.model.contract.ContractPayment;
import com.ptswitch.material.model.security.Role;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.ContractPaymentService;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.util.ContractUtil;
import com.ptswitch.material.util.MaterialUtil;
import com.ptswitch.material.util.security.PermissionUtil;

public class AskPayment extends PageBase {
    @Inject
    private ContractService contractSer;
    @Inject
    private ContractPaymentService contractPaymentSer;
    @Property
    private Contract contract;
    @Property
    private ContractPayment payment;
    //
    @InjectPage
    private ViewContract viewContract;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(long id) {
        contract = contractSer.findById(id);
        checkPermitted(PermissionUtil.ContractPaymentAsk);
        if (contract.isContractOk()) {
            setMessage("合同已完成");
            return viewContract.withInit(contract.getId());
        }
        payment = new ContractPayment();
        payment.setReciever(contract.getFinancial());
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return contract.getId();
    }

    @OnEvent(value = EventConstants.PREPARE)
    void prepareValidateRestrict() {
//		payment.setPayment(BigDecimalUtil.subtract(contract.getTotalPrice(), contract.getAccountPaid()));
        payment.setPaymentCurrentMax(BigDecimalUtil.subtract(contract.getTotalPrice(), contract.getAccountPaid()));
        if (BigDecimalUtil.equals(contract.getAccountPaid(), 0)) {
            payment.setPaymentCurrent(BigDecimalUtil.multiply(contract.getTotalPrice(), 30, 100));
        } else {
            payment.setPaymentCurrent(payment.getPaymentCurrentMax());
        }
        payment.setPayment(payment.getPaymentCurrent());
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "askPayment")
    Object addPayment() {
        checkPermitted(PermissionUtil.ContractPaymentAsk);
        //check payment
        if (BigDecimalUtil.biggerThan(payment.getPayment(), BigDecimalUtil.subtract(contract.getTotalPrice(), contract.getAccountPaid()))) {//FIXME
            throw AppRuntimeException.error("申请的金额大于合同所需");
        }
        //save
        payment.setSender(getVisitor());
        payment.setContract(contract);
        payment.setPaymentType(ContractPayment.Askfor_Payment);
        payment.setCreateTime(new Date());
        contractPaymentSer.save(payment);
        //
        contract.updateForAddPayment(payment);
        contractSer.save(contract);
        //send email
        EMail email = MaterialUtil.newEMail();
        email.addTo(payment.getReciever().getEmail());
        email.setSubject(payment.getSender().getRealname() + "向你申请付款:" + payment.getPayment());
        email.addHtml(MaterialUtil.linkForViewContract(getWebUrl(), contract));
        email.addText("申请付款金额：" + payment.getPayment());
        email.addText("申请付款备注：" + payment.getRemark());
        email.addHtml(MaterialUtil.linkForReplyPayment(getWebUrl(), payment));
        EMailUtil.send(email);
        return viewContract.withInit(contract.getId());
    }

    @Override
    protected List<Role> appendRoleToVisitor(Uzer visitor) {
        return ContractUtil.appendThisRole(contract, visitor);
    }
}
